/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
*/

package nixonftp.list;

import nixonftp.*;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JToolTip;

public class NXQueueRenderer extends JLabel implements TableCellRenderer {
	private final boolean isBordered;

	public NXQueueRenderer(boolean isBordered) {
		this.isBordered = isBordered;
		setOpaque(true); //MUST do this for background to show up.
	}

	public Component getTableCellRendererComponent(
			JTable table, Object obj,
			boolean isSelected, boolean hasFocus,
			int row, int column) {
        
		if (isBordered) {
			if (isSelected) {
				//selectedBorder is a solid border in the color
				//table.getSelectionBackground().
				setBackground(table.getSelectionBackground());
				setForeground(table.getSelectionForeground());
			} else {
				//unselectedBorder is a solid border in the color
				//table.getBackground().
				setBackground(table.getBackground());
				setForeground(table.getForeground());
			}
		}

		NXQueueItem idx = (NXQueueItem) obj;

		setIcon(null);
		setIconTextGap(8);
		setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
		this.setToolTipText(null);

		switch (column) {
			case 0:
				ImageIcon i = getIcon(idx, row == 0);
				if (i != null) {
					setIcon(i);
					setText(null);
				} else {
					setText(idx.activity);
				}
				setToolTipText(idx.activity);
				break;
			case 1:
				if (idx.fileName.isEmpty()) {
					setText(idx.remoteFolder);
					setToolTipText(idx.remoteFolder);
				} else {
					setText(idx.fileName);
					String tipText = "<html><font color='#336699'><b>" + idx.fileName +
						  "</b></font><br><font color='#666666'><b>Remote folder: </b>" + idx.remoteFolder +
						  "</font><br><b>Local folder: </b>" + idx.localFolder + "</html>";
					setToolTipText(tipText);
				}
				break;
			case 2:
				String strSize = NXFormat.fileSize(idx.totalSize, true);
				setText(strSize);
				setToolTipText(strSize);
				break;
		}

		return this;
	}

	private ImageIcon getIcon(NXQueueItem qi, boolean current) {
		ImageIcon i = null;
		String activity = qi.activity;
		if (activity.equals("download")) {
			return (current) ? NXIcon.downloading : NXIcon.download;
		} else if (activity.equals("upload")) {
			return (current) ? NXIcon.uploading : NXIcon.upload;
		} else if (activity.contains("delete") || (qi.activity.equals("command") && qi.localFolder.startsWith("RMD"))) {
			return NXIcon.trash;
		} else if (activity.equals("index") || activity.equals("list")) {
			return NXIcon.list;
		} else if (activity.startsWith("mkdir")) {
			return NXIcon.addFolder;
		} else if (activity.equals("preload")) {
			return NXIcon.small[1];
		}
		return i;
	}
}
